#include "stdafx.h"
#include "imageProcessing.h"
#include <stdio.h>
#include <math.h>
#include "cv.h"
#include "highgui.h"
#include "transformation.h"
#include "operatoriPuntuali.h"
#include <windows.h>
#include <iostream>

using namespace System;
using namespace System::Runtime::InteropServices;

double run_directory_process(System::String^ tempOriginal, System::String^ tempFinal,int LimitMAX,int LimitMIN,int windowLimitUp,int windowLimitDown)
{
	System::IO::Directory::CreateDirectory(tempFinal);
	char* original = (char*)(void*)Marshal::StringToHGlobalAnsi(tempOriginal);
	char* final = (char*)(void*)Marshal::StringToHGlobalAnsi(tempFinal);


	return directory_process(original,final,LimitMAX,LimitMIN,windowLimitUp,windowLimitDown);
}

double run_image_process(System::String^ tempOriginal, System::String^ tempFinal,int LimitMAX,int LimitMIN,int windowLimitUp,int windowLimitDown)
{
	char* original = (char*)(void*)Marshal::StringToHGlobalAnsi(tempOriginal);
	char* final = (char*)(void*)Marshal::StringToHGlobalAnsi(tempFinal);


	return image_process(original,final,LimitMAX,LimitMIN,windowLimitUp,windowLimitDown);
}

double directory_process(char* originalDirectory,char* finalDirectory, int LimitMAX,int LimitMIN,int windowLimitUp,int windowLimitDown){
	

	IplImage* image_in,*image_out,*image_out2,*image_out3,*image_out4,*image_bin;
	int MIN,MAX;
	int w,ws,h;
	IplConvKernel* kernel;
	char fileName[128];
	char directoryFileName[256];
	double result=0,result_temp;
	int labels;
	int takens=0;

	for(int i=0;i<15;i++){
		if(i<10)
			sprintf_s(fileName,"/IMG_000%d.bmp",i);
		else
			sprintf_s(fileName,"/IMG_00%d.bmp",i);
		sprintf_s(directoryFileName,"%s%s",originalDirectory,fileName);
		image_in = cvLoadImage(directoryFileName,0);
		
		sprintf_s(directoryFileName,"%s%s",finalDirectory,fileName);

		if(image_in==0)
			return -1;
 
		w = image_in->width;
		ws = image_in->widthStep;
		h = image_in->height;

		image_out2=cvCreateImage(cvGetSize(image_in), IPL_DEPTH_8U, 1);

		//prova gammaCorrection
		if(i==12 || i==13 || i==14){
			image_in=gammaCorrection(image_in,0.6);
		}
		//fine - prova

		image_bin=binarizzazione(image_in);
	
		image_out=takeOeliminateV(image_bin,LimitMAX,LimitMIN,windowLimitUp);
/*
		cvNamedWindow("progetto2", 0);
		cvResizeWindow("progetto2",w,h);
		cvShowImage("progetto2", image_out);
		printf("premi un tasto per uscire.\n");
		cvSaveImage("./progetto2.jpg", image_out);
		cvWaitKey(0);
*/
		MAX=windowLimits(image_out,RIGHT,windowLimitUp);
		MAX-=LimitMAX;
	
		MIN=windowLimits(image_out,LEFT,windowLimitUp);
		MIN+=LimitMIN;

		image_out2=takeOnlyInside(image_out,windowLimitUp,h,MIN,MAX);



		result_temp=liquid_level(image_out2);


		image_out3=takeOnlyInside(image_out,windowLimitUp,windowLimitDown,MIN,MAX);

		//cvCanny(image_out,image_out2,50,100,7);
		image_out2=takeBorder(image_out3);

		kernel=cvCreateStructuringElementEx(7,7,3,3,CV_SHAPE_RECT, NULL );

		cvDilate( image_out2, image_out3, kernel,1 );

		cvReleaseStructuringElement(&kernel);

		image_out4=cvCreateImage(cvGetSize(image_in), IPL_DEPTH_8U, 1);

		labels=labeling(image_out3,image_out4);
		
		if(labels!=0){
			cvReleaseImage(&image_out3);

			image_out3=findMostImportant(image_out4);

			cvReleaseImage(&image_out2);

			image_out2=colorBorderAndScale(image_in,image_out3,BLACK);

			result+=result_temp;
			takens++;
			
		}
		else{	
			cvReleaseImage(&image_out2);

			image_out2=scaleImage(image_in,8,false);
		}

		cvSaveImage(directoryFileName,image_out2);

		
		cvReleaseImage(&image_in);
		cvReleaseImage(&image_out);
		cvReleaseImage(&image_bin);
		cvReleaseImage(&image_out2);
		cvReleaseImage(&image_out3);
		cvReleaseImage(&image_out4);

	}
	result=result/takens;

	return result;
}


double image_process(char* originalFile,char* finalFile, int LimitMAX,int LimitMIN,int windowLimitUp,int windowLimitDown){
	

	IplImage* image_in,*image_out,*image_out2,*image_bin;
	int MIN,MAX;
	int w,ws,h;
	IplConvKernel* kernel;

	image_in = cvLoadImage(originalFile,0);
	if(image_in==0)
		return -1;
 
	w = image_in->width;
	ws = image_in->widthStep;
	h = image_in->height;

	image_bin=cvCreateImage(cvGetSize(image_in), IPL_DEPTH_8U, 1);
	image_out=cvCreateImage(cvGetSize(image_in), IPL_DEPTH_8U, 1);
	image_out2=cvCreateImage(cvGetSize(image_in), IPL_DEPTH_8U, 1);

	image_bin=binarizzazione(image_in);
	
	image_out=takeOeliminateV(image_bin,LimitMAX,LimitMIN,windowLimitUp);

	MAX=windowLimits(image_out,RIGHT);
	MAX-=LimitMAX;
	
	MIN=windowLimits(image_out,LEFT);
	MIN+=LimitMIN;

	image_out=takeOnlyInside(image_out,windowLimitUp,windowLimitDown,MIN,MAX);

	cvCanny(image_out,image_out2,50,100,7);

	kernel=cvCreateStructuringElementEx(7,7,3,3,CV_SHAPE_RECT, NULL );

	cvDilate( image_out2, image_out, kernel,1 );

	cvReleaseStructuringElement(&kernel);

	image_out2=colorBorderAndScale(image_in,image_out,WHITE);

	cvSaveImage(finalFile,image_out2);

	cvReleaseImage(&image_in);
	cvReleaseImage(&image_out);
	cvReleaseImage(&image_bin);
	cvReleaseImage(&image_out2);

	return 0;
}